package edu.drexel.cs.eVoter.VoterClient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;

public class CandidateResultsPanel extends ClientAbstractPanel 
	implements CandidateListener 
{
	private static final long serialVersionUID = -538777222073956027L;

	private JLabel            candidateLabel = new JLabel( "Choose Candidate:" );
	private JComboBox<String> candidateComboBox;
	private JLabel            tallyLabel = new JLabel( "Votes:" );
	private JLabel    		  counts;
	
	//private String[]          stateStrings = {"Alabama","Alaska","American Samoa","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Guam","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Northern Marianas Islands ","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia ","Virgin Islands ","Washington","West Virginia","Wisconsin","Wyoming "};;
	private static String[]     candidateStrings = {"Mitt Romney", "Barack Obama", "Iron Man", "Hulk", "Jane Doe"};
	//private String[]          voterStrings = { "Somebody 1", "Somebody 2", "Somebody 3", "Somebody 4", "Somebody 5" };
	
	public CandidateResultsPanel(ServerInterface si) 
	{
		super(si);
		
		si.addCandidateListener( this );
		Box mainbox, b;
		
		mainbox = Box.createVerticalBox();
		
		b = Box.createHorizontalBox();
		b.add( candidateLabel );
		candidateComboBox = new JComboBox<String>( candidateStrings );
		candidateComboBox.addActionListener( new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				processCandidateSelection( e );
			}
		});
		b.add( candidateComboBox );
		mainbox.add( b );
		
		b = Box.createHorizontalBox();
		b.add( tallyLabel );
		counts = new JLabel("0");
		b.add( counts );
		mainbox.add( b );
		
		add( mainbox );
	}

	private void processCandidateSelection( ActionEvent event )
	{
		myServerInterface.candidateResults( candidateComboBox.getSelectedItem().toString() );
	}
	
	@Override
	public void candidateData(String candidate, int votes) 
	{
		//if ( candidate.compareTo( candidateComboBox.getSelectedItem().toString() ) == 0 )
		//{
			counts.setText( Integer.toString( votes ) );
		//}
		//else
		//{
			// why are we here?????
			//System.out.println("Warning: candidates didn't match in return message for candidateData");
		//}
	}

	public static Map<String, Integer> getCandidateInitialVotes()
	{
		Map<String, Integer> returnMap = new HashMap<String, Integer>();
		
		for(String candidateName : candidateStrings)
		{
			returnMap.put(candidateName, 0);
		}
		
		return returnMap;
	}
	
}
